package liangtWorkSpace.threadDemo.thread.JUC;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author liangt
 * @ClassName: Condition
 * @title: Condition
 * @projectName liangtWorkSpace.threadDemo.thread.JUC
 * @description: 条件唤醒--达到条件可以唤醒锁，完成指定顺序调度
 * @date 2020/11/5 19:10
 */
public class ConditionSimple {
    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        Condition condition1 = lock.newCondition();
        Condition condition2 = lock.newCondition();
        Condition condition3 = lock.newCondition();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    lock.lock();
                    condition1.await();
                    System.out.println("粒粒皆辛苦");

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }

            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    lock.lock();
                    condition2.await();
                    System.out.println("谁知盘中餐");
                    condition1.signal();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }

            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    lock.lock();
                    condition3.await();
                    System.out.println("汗滴禾下土");
                    condition2.signal();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }
            }
        }).start();


        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    lock.lock();
                    System.out.println("锄禾日当午");
                    condition3.signal();
                }catch (Exception e){

                }finally {
                    lock.unlock();
                }
            }
        }).start();
    }
}